package cn.tingting.service.impl;

import cn.tingting.cilent.SysCilent;
import cn.tingting.client.CourseDocClient;
import cn.tingting.context.UserContext;
import cn.tingting.context.UserContextHolder;
import cn.tingting.domain.Course;
import cn.tingting.domain.CourseDetail;
import cn.tingting.domain.CourseMarket;
import cn.tingting.domain.CourseType;
import cn.tingting.dto.CourseDto;
import cn.tingting.enums.CourseGrade;
import cn.tingting.esdoc.CourseDoc;
import cn.tingting.mapper.CourseDetailMapper;
import cn.tingting.mapper.CourseMapper;
import cn.tingting.mapper.CourseMarketMapper;
import cn.tingting.mapper.CourseTypeMapper;
import cn.tingting.query.CourseDocQuery;
import cn.tingting.query.CourseQuery;
import cn.tingting.service.ICourseService;
import cn.tingting.util.AjaxResult;
import cn.tingting.util.PageList;
import cn.tingting.util.ValidUtils;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ztt
 * @since 2021-03-05
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseDetailMapper courseDetailMapper;
    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Autowired
    private CourseDocClient client;
    @Autowired
    private CourseTypeMapper courseTypeMapper;


    @Override
    public PageList<Course> ListCourse(CourseQuery query) {
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        System.out.println(page);
        List<Course> date =  courseMapper.ListCourse(page,query);

        return new PageList<>(page.getTotal(),date);
    }

    @Override
    public void insertplus(CourseDto courseDto) {
        Course course = courseDto.getCourse();
        course.setStatus(0);
        course.setCommentCount(0);
        course.setViewCount(0);
        course.setSaleCount(0);
        course.setGradeName(CourseGrade.getCourseGrade(courseDto.getCourse().getGradeId()).getName());
        courseMapper.insert(course);
        CourseMarket courseMarket = courseDto.getCourseMarket();
        courseMarket.setId(courseDto.getCourse().getId());
        courseMarket.setCharge(courseDto.getCourseMarket().getCharge());
        courseMarket.setExpires(courseDto.getCourseMarket().getExpires());
        courseMarket.setPrice(courseDto.getCourseMarket().getPrice());
        courseMarket.setPriceOld(courseDto.getCourseMarket().getPriceOld());
        courseMarket.setQq(courseDto.getCourseMarket().getQq());
        courseMarketMapper.insert(courseMarket);
        CourseDetail courseDetail = courseDto.getCourseDetail();
        courseDetail.setId(courseDto.getCourse().getId());
        courseDetail.setDescription(courseDto.getCourseDetail().getDescription());
        courseDetail.setIntro(courseDto.getCourseDetail().getIntro());
        courseDetailMapper.insert(courseDetail);

    }

    @Override
    public void deleteByIdplus(Long id) {
        courseMapper.deleteById(id);
        courseDetailMapper.deleteById(id);
        courseMarketMapper.deleteById(id);
    }

    @Override
    public AjaxResult onLineCourse(Long id, boolean b) {
        AjaxResult result;
        if(b){
            //上线、
            Course course = courseMapper.selectById(id);
            CourseType courseType = courseTypeMapper.selectById(course.getCourseTypeId());
            ValidUtils.IntEuqal(course.getStatus(),1 , "该课程已经上线");

            CourseDoc courseDoc =  courseToDoc(course);
            System.out.println(courseDoc.toString());
            courseDoc.setTenantName(courseType.getName());
             result = client.addorupdate(courseDoc);
            System.out.println(result.toString());
             ValidUtils.assertBoolean(result.isSuccess(), "创建索引库失败");
            course.setStatus(1);
            course.setOnlineTime(new Date());
            UserContext context = UserContextHolder.context();
            course.setTenantId(context.getTenantId());
            course.setTenantName(context.getTenantame());
            course.setUserId(context.getUserId());
            course.setUserName(context.getUsername());
            courseMapper.updateById(course);
            return  result;
            //判断
        }else {
            //下线
            Course course = courseMapper.selectById(id);
            ValidUtils.IntEuqal(course.getStatus(),0, "该课程已经下线");
            result = client.dele(course.getId());
            ValidUtils.assertBoolean(result.isSuccess(), "删除索引库失败");
            course.setStatus(0);
            course.setOfflineTime(new Date());
            courseMapper.updateById(course);

             return result;
            //判断
        }
    }

    @Override
    public PageList<CourseDoc> queryCourses(CourseDocQuery query) {
        return client.queryCourses(query);
    }

    private CourseDoc courseToDoc(Course course) {
        CourseDoc courseDoc = new CourseDoc();
        BeanUtils.copyProperties(course,courseDoc );
        CourseMarket courseMarket = courseMarketMapper.selectById(course.getId());
        BeanUtils.copyProperties(course, courseMarket);
        CourseDetail courseDetail = courseDetailMapper.selectById(course.getId());
        BeanUtils.copyProperties(course, courseDoc);
         return   courseDoc;
    }

    @Override
    public void updateByIdplus(CourseDto courseDto) {
        Course course = courseDto.getCourse();
        course.setGradeName(CourseGrade.getCourseGrade(courseDto.getCourse().getGradeId()).getName());
        courseMapper.updateById(course);
        courseDto.getCourseDetail().setId(courseDto.getCourse().getId());
        courseDetailMapper.updateById(courseDto.getCourseDetail());
        courseDto.getCourseMarket().setId(courseDto.getCourse().getId());
        courseMarketMapper.updateById(courseDto.getCourseMarket());

    }


}
